"""
helper.py

Provides some helper functions.
"""



import random



def choice(some_list, probabilities, max_probability=1):
    """Provides a method to pick an item at random from a list,
    but with different probabilities given in another list."""
    x = random.uniform(0, max_probability)
    cumulative_probability = 0.0

    for item, item_probability in zip(some_list, probabilities):
        cumulative_probability += item_probability
        if x < cumulative_probability: break

    return item


def make_vector(start, finish):
    """Creates a vector given a start and a finish point."""
    init = complex(start[0], start[1])
    end  = complex(finish[0], finish[1])

    vector = end - init
    return vector


def str2tuple(str):
    """Transforms a string representation of a tuple of two integers
    into a tuple of two integers."""
    pack = (int(str[1]), int(str[4]))
    return pack


def slice(list, point):
    """Break a list where 'point' occurs, into two sublists
    without including 'point'"""
    index  = list.index(point)
    slices = []
        
    slices.append(list[:index])
    slices.append(list[index + 1:])
        
    return slices


def random_color():
    color_choices = ['0','1','2','3','4','5','6','7','8',
                     '9','A','B','C','D','E','F']
        
    return  "#" + "".join(random.sample(color_choices, 6))


def normalize(list, max_value=1):
    """Normalize a list of values."""
    maxi = max(list)
    mini = min(list)

    if maxi == mini or len(list) == 1:
        return list
    
    norm = []

    for item in list:
        new = max_value * ((item - mini) / (maxi - mini))
        norm.append(new)

    return norm
